package com.CMUBiS;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.CMUBiS.Data.GlobalVars;
import com.CMUBiS.util.Item;

@WebServlet(name="SearchCategoryServlet", urlPatterns="/searchcategory")
public class SearchCategoryServlet extends HttpServlet{
	private static final long serialVersionUID = 6472741006723438743L;
	private static final String EMPTY = "";
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// Non registered users can use this page, so they don't need to send any parameters
		this.makePage(request, response, "");
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param errorString
	 * @throws ServletException
	 * @throws IOException
	 */
	public void makePage(HttpServletRequest request, HttpServletResponse response, String errorString)  throws ServletException, IOException {
		// We are using PrintWriter to be friendly to the international community. PrintWrite auto-converts coding
		PrintWriter out = response.getWriter();
		
		// Include header
		
		
		// Do HTML4 or 5 depending on the global variables
		if(GlobalVars.isHTML4){
			out.println("<HTML>");
			out.println("<HEAD><TITLE>Browse category</TITLE></HEAD>");
			out.println("<BODY>");
			out.println(errorString);
			createHTML4(request, response, out);
		}else{
			createHTML5(request, response, out);
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 * @param out
	 * @return
	 */
	public HttpServletResponse createHTML4(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			// Set the return type
			response.setContentType("text/html");
			
			// Get the URL parameters if any were passed
			Map<String, String[]> paramMap = request.getParameterMap();
			
			String userID="-1";
			String authToken="-1";
			String text = "";
			
			if(paramMap.containsKey("userID"))
				userID=request.getParameter("userID");
			if(paramMap.containsKey("authToken"))
				authToken=request.getParameter("authToken");
			if(paramMap.containsKey("text")){
				try{
					text=request.getParameter("text");
				}
				catch(Exception e){
					text = "";
				}
			}
			
			/*
			 * Print all of the current category and sub-categories
			 */
			//TODO: make this a side bar
			
			out.println("<div id=\"left\">");
			
			// Print the search box
			out.println("<div id=\"center\">");
			out.println("<form name=\"search\" action=\"searchcategory\" method=\"POST\">");
			out.println("<div id=\"floatright\">Search: </div>");
			out.println("<div id=\"floatcenter\"> <input type=\"hidden\" name=\"userID\" size=\"30\" value=\""+ (paramMap.containsKey("userID") ?  request.getParameter("userID") : "") + "\"/> </div>");
			out.println("<div id=\"floatcenter\"> <input type=\"hidden\" name=\"authToken\" size=\"30\" value=\""+ (paramMap.containsKey("authToken") ?  request.getParameter("authToken") : "") + "\"/> </div>");
			out.println("<div id=\"floatcenter\"> <input type=\"text\" name=\"text\" size=\"30\" value=\""+ (paramMap.containsKey("text") ?  request.getParameter("text") : "") + "\"/> </div>");
			
			out.println("<div id=\"floatright\"><input type=\"submit\" value=\"Search\"></div>");
			
			out.println("");
			out.println("</form>");
			out.println("</div>");
			
		
			
			/*
			 * Print out all of the items in the current category	
			 */
			ArrayList<Item> items = GlobalVars.db.getTextItems(text);
			
			for(Item item:items){
				out.println("<div id=\"entry\">");
				out.println("<a href=\"viewitem?userID=" + userID + "&authToken="+authToken + "&itemID=" + item.getID() + "\">");
				out.println(item.getDescription());
				out.println("</a>");
				out.println(item.getCurrentBid());
				out.println(item.getEndDate());
				out.println("</div>");
			}
			
			
			// Include footer
			out.println("</BODY></HTML>");
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	public HttpServletResponse createHTML5(HttpServletRequest request, HttpServletResponse response, PrintWriter out){
		try{
			
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
		return response;
	}
}
